package com.changgou.goods.service.impl;

;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.dao.BrandDao;

import com.changgou.goods.service.BrandService;
import com.haha.goods.entity.Brand;
import com.changgou.framework.entity.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/5
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandDao, Brand> implements BrandService {
    @Override
    public List<Brand> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Brand> getByBrand(Brand brand) {
        //判断条件是否为空，然后加入查询的条件里面
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(brand.getId()),"id",brand.getId());
        queryWrapper.eq(!StringUtils.isEmpty(brand.getSeq()),"seq",brand.getSeq());
        queryWrapper.like(!StringUtils.isEmpty(brand.getName()),"name",brand.getName());
        queryWrapper.like(!StringUtils.isEmpty(brand.getImage()),"image",brand.getImage());
        queryWrapper.like(!StringUtils.isEmpty(brand.getLetter()),"letter",brand.getLetter());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public PageResult findPage(Brand brand, int page, int size) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if(brand!=null) {
            queryWrapper.eq(!StringUtils.isEmpty(brand.getId()), "id", brand.getId());
            queryWrapper.eq(!StringUtils.isEmpty(brand.getSeq()), "seq", brand.getSeq());
            queryWrapper.like(!StringUtils.isEmpty(brand.getName()), "name", brand.getName());
            queryWrapper.like(!StringUtils.isEmpty(brand.getImage()), "image", brand.getImage());
            queryWrapper.like(!StringUtils.isEmpty(brand.getLetter()), "letter", brand.getLetter());
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        int offset = (page-1)*size;
        List<Brand> brands = baseMapper.selectListByPage(queryWrapper,offset,size);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRows(brands);
        return pageResult;
    }

    @Override
    public List<Brand> findByCategory(Integer categoryId) {
        List<Brand> brands = baseMapper.findByCategory(categoryId);
        return brands;
    }
}
